﻿using MesBSW.DAL.RoleDB;
using MesBSW.Tools;
using MesBSW.View;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Windows.Forms;


namespace MesBSW.BLL.Role
{
    public class MenuControler
    {

        public class Frm { 
            public string id { get; set; } 
            public string name { get; set; } 
        }

        public List<TreeNode> getMenuTreeNodes(int pid = 0)
        {
            var menuNode = new List<TreeNode>();
            using (RoleDB db = new RoleDB())
            {
                var menus = db.base_menu.Where(u => u.parent_id == pid && u.base_prog_id == MainFrm.BaseProg.id)
                            .OrderBy(u => u.sort)
                            .ToList();

                foreach (var menu in menus)
                {
                    TreeNode node = new TreeNode();
                    node.Name = menu.id.ToString();
                    node.Text = menu.menu_name.ToString();
                    node.Toggle();
                    node.Nodes.AddRange(getMenuTreeNodes(menu.id).ToArray());
                    menuNode.Add(node);
                }
            }

            return menuNode;
        }

        public List<BaseMenuModel> getMenus(int pid = 0,int level=0, List<BaseMenuModel> menuList=null)
        {
            if (menuList==null) 
            {
                menuList = new List<BaseMenuModel>();
            }
            using (RoleDB db = new RoleDB())
            {
                var menus = db.base_menu.Where(u => u.parent_id==pid && u.base_prog_id == MainFrm.BaseProg.id)
                            .OrderBy(u => u.sort)
                            .ToList();

                //输出层数
                string levels = "";
                for (int i = 0; i < level; i++)
                {
                    levels += "|-";
                }

                foreach (var menu in menus)
                {                  
                    menu.menu_name = levels+menu.menu_name.ToString();
                    menuList.Add(menu);
                    getMenus(menu.id, level+1, menuList);
                }
            }

            return menuList;
        }


        public BaseMenuModel getMenu(int id)
        {
            BaseMenuModel menu = new BaseMenuModel();

            using (RoleDB db = new RoleDB())
            {
                 menu = db.base_menu.Where(u=>u.id==id).FirstOrDefault();
               
            }
            return menu;
        }


        public List<Frm> getFrm()
        {
            List<Frm> frms = new List<Frm>();
            //.net中的反射  Application.ExecutablePath exe所在路径   
            System.Reflection.Assembly ase = System.Reflection.Assembly.LoadFile(Application.ExecutablePath);
            Type[] types = ase.GetTypes();
            foreach (Type item in types)  //获取程序中的form窗体
            {
                if (item.FullName.Contains("Frm") 
                    && !item.FullName.Contains("<>")
                    && !item.FullName.Contains("+"))
                {
                    frms.Add(new Frm { id=item.FullName,name = item.FullName });
                }
            }

            return frms.OrderBy(q => q.name).ToList();
        }


        public void addMenus(BaseMenuModel baseMenuModel) 
        {
            using (RoleDB db = new RoleDB())
            {
               var tran = db.Database.BeginTransaction();

                try
                {
                    var tmpid = baseMenuModel.id;
                    if (baseMenuModel.id == 0)
                    {
                        baseMenuModel.create_user = MainFrm.LoginUser.user_name;
                        baseMenuModel.create_date = DateTime.Now;
                        baseMenuModel.base_prog_id = MainFrm.BaseProg.id;
                        baseMenuModel.is_del = false;
                        baseMenuModel.menu_code = db.Database.SqlQuery<string>("getLsh @p",
                                            new SqlParameter("@p", "Menu")).First();
                        db.base_menu.Add(baseMenuModel);
                    }
                    else
                    {
                        var menu = db.base_menu.Where(u => u.id == baseMenuModel.id).FirstOrDefault();
                        if (menu != null)
                        {
                            menu.menu_name = baseMenuModel.menu_name;
                            menu.class_name = baseMenuModel.class_name;
                            menu.parent_id = baseMenuModel.parent_id;
                            menu.sort = baseMenuModel.sort;
                        }
                    }

                    var res = db.SaveChanges();
                    SysTools.errCode = res;

                    if (res > 0)
                    {
                        tran.Commit();
                    }
                    else { 
                        tran.Rollback();
                    }

                    if (tmpid == 0)
                    {
                        SysTools.errMsg = res > 0 ? "菜单添加成功" : "菜单添加失败";
                    }
                    else
                    {
                        SysTools.errMsg = res > 0 ? "菜单修改成功" : "菜单修改失败";
                    }
                }
                catch (Exception) 
                {
                    tran.Rollback();
                }
            }

        }

        public void delMenus(int id) 
        {

            using (RoleDB db = new RoleDB())
            {
                var tran = db.Database.BeginTransaction();
                try 
                { 
                   var menu = db.base_menu.Where(u => u.id ==id).FirstOrDefault();
                   db.base_menu.Remove(menu);
                   var res = db.SaveChanges();

                    if (res > 0)
                    {
                        tran.Commit();
                    }
                    else
                    {
                        tran.Rollback();
                    }

                    SysTools.errCode = res;
                    SysTools.errMsg = res > 0 ? "菜单删除成功" : "菜单删除失败";
                }
                catch (Exception) {
                    tran.Rollback();
                }
            }

        }

    }
}
